﻿namespace System {
    public static class HttpContextCore
    {
        public static HttpContext Current { get => ServiceLocator.GetServiceProvider().GetService<IHttpContextAccessor>().HttpContext; }
        /// <summary>
        /// 获取上下文值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public static T GetItem<T>(string key)
        {
            Current.Items.TryGetValue(key, out object res);
            return res.To<T>();
        }
        /// <summary>
        /// 设置上下文值
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public static void SetItem(string key, string value)
        {
            if (Current.Items.ContainsKey(key))
            {
                Current.Items.Remove(key);
            }
            Current.Items.Add(key, value);
        }
        /// <summary>
        /// 移除上下文值
        /// </summary>
        /// <param name="key"></param>
        public static void RemoveItem(string key)
        {
            if (Current.Items.ContainsKey(key))
            {
                Current.Items.Remove(key);
            }

        }
    }
}
